/* jQuery.checkform.js
 *
 * check type:
 *  mustInput
 *  mustMoreThan
 *  mustLessThan
 *  mustEqualTo
 *  mustEmail
 *  mustInt
 *  mustFloat
 *  mustSelect
 *  mustCheck
 *  mustRadio:
 *  mustRegular
 *
 */ 
 
jQuery.extend({	
    options: {
        ctrls: [ ],//controls to check         
        success: function() { return; },//When check success,you can do something,such as submit a ajax request
        failed:  function(msg, id){ jQuery.clewMsg(msg, id); } //when check faild                
    },
	
	clewMsg: function(msg, id){
	    alert(msg);
	},
	
	checkForm: function(o){	 
		o = jQuery.extend({},jQuery.options,o);
		
	    var isok = true;
	    
	    
	    var flashPrompt = function(ctr){
			var i = 0;			
			var intervalid = setInterval(function(){ 			    
				jQuery("#"+ctr.id).toggleClass('warning');
				if(i++ > 2){
					clearInterval(intervalid);
					jQuery("#"+ctr.id).addClass('warning');
				}
			}, 100);			
		};	
		
	    //检查错误，提示信息并高亮显示
	    var fail = function(ctr){
	    		jAlert(ctr.msg, '提示信息');
	            isok = false; 
                o.failed(ctr.msg, ctr.id);
                flashPrompt(ctr);
                jQuery("#"+ctr.id).focus();
                return false;
	        }
	    
	    //检查成功去掉高亮显示
	    var succ = function(ctr){
	            jQuery("#"+ctr.id).removeClass('warning');
	            return true;
	        }
	    
	    //正则表达式检查
	    var checkRegularExpression = function(val, expression){
	            if(val != "")
                {
                    var matchArray = val.match(expression);
                    if (matchArray == null)return false;else return true;
                }
                else return true;
	        }
	        		
		jQuery.each(o.ctrls, function(i, ctr){
			
		    switch(ctr.type)
		    {
		        case "mustInput":   if(jQuery("#"+ctr.id).val() == "")return fail(ctr); else return succ(ctr);
		        case "checkInput":  if(!checkRegularExpression(jQuery("#"+ctr.id).val(), /^[a-zA-Z]([a-zA-Z0-9]|[._]){4,19}$/))return fail(ctr); else return succ(ctr);
		        case "checkblank":  if(checkRegularExpression(jQuery("#"+ctr.id).val(), /(^\s*)|(\s*$)/))return fail(ctr); else return succ(ctr);
		        case "nochinese":   if(!checkRegularExpression(jQuery("#"+ctr.id).val(), /[^\u4E00-\u9FA5]/))return fail(ctr); else return succ(ctr);
		        case "mustPassword":if(!checkRegularExpression(jQuery("#"+ctr.id).val(), /^(\w){6,20}$/))return fail(ctr); else return succ(ctr);
		        case "mustMoreThan":if(jQuery("#"+ctr.id).val().length < ctr.par)return fail(ctr); else return succ(ctr);
		        case "mustLessThan":if(jQuery("#"+ctr.id).val().length > ctr.par)return fail(ctr); else return succ(ctr);
		        case "mustEqualTo": if(jQuery("#"+ctr.id).val() != jQuery("#"+ctr.par).val())return fail(ctr);else return succ(ctr);
		        case "mustEmail":   if(!checkRegularExpression(jQuery("#"+ctr.id).val(), /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/))return fail(ctr);else return succ(ctr);
                case "mustInt":     if(!checkRegularExpression(jQuery("#"+ctr.id).val(), /^[0-9]*$/))return fail(ctr);else return succ(ctr); 
                case "mustFloat":   if(!checkRegularExpression(jQuery("#"+ctr.id).val(), /^[0-9]+\.{0,1}[0-9]{0,2}$/))return fail(ctr);else return succ(ctr); 
		        case "mustSelect":  if(jQuery("#"+ctr.id).val() == ctr.par)return fail(ctr); else return succ(ctr);
		        case "mustCheck":   if(!jQuery("#"+ctr.id).attr("checked"))return fail(ctr); else return succ(ctr);
		        case "mustRadio":   if(jQuery("input[type='radio'][name='"+ctr.name+"']:checked").length<1)return fail(ctr); else return succ(ctr);
		        case "mustRegular": if(!checkRegularExpression(jQuery("#"+ctr.id).val(), ctr.par))return fail(ctr);else return succ(ctr);
		        case "mustPhone":   if(!checkRegularExpression(jQuery("#"+ctr.id).val(), /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/))return fail(ctr);else return succ(ctr); 
		    }
		    });
		
		if(isok) o.success();
	}	
});